<script>
	import { login } from './api/user.js'
	import { useUserStore } from './store/user.js'
	import { ensureValidToken, refreshAccessToken } from './utils/request.js'
	
	export default {
		onLaunch: async function() {
			console.log('App Launch')
			
			// 自动登录逻辑
			try {
				// 检查是否已经有有效的token
				const token = uni.getStorageSync('token')
				const userInfo = uni.getStorageSync('userInfo')
				
				if (token && userInfo) {
					console.log('检测到已保存的登录信息，自动登录')
					// 恢复用户状态到store
					const userStore = useUserStore()
					userStore.setToken(token)
					userStore.setUserInfo(userInfo)
					// 启动后台token保活
					this.startTokenAutoRefresh()
					
					// 直接跳转到主页
					uni.reLaunch({
						url: '/pages/index/index'
					})
					return
				}
				
				// 如果没有保存的登录信息，使用默认账号自动登录
				console.log('使用默认账号自动登录')
				const defaultCredentials = {
					Username: 'admin7788',
					Password: '123456'
				}
				
				// 显示加载提示
				uni.showLoading({
					title: '自动登录中...'
				})
				
				// 调用登录API
				const res = await login(defaultCredentials)
				const tokenData = res?.data || res
				console.log('自动登录响应:', res)
				
				if (tokenData?.accessToken) {
					// 保存认证信息
					const userStore = useUserStore()
					userStore.setAuth(tokenData)
					
					// 保存到本地存储
					uni.setStorageSync('token', tokenData.accessToken)
					if (tokenData.refreshToken) {
						uni.setStorageSync('refreshToken', tokenData.refreshToken)
					}
					if (tokenData.user) {
						uni.setStorageSync('userInfo', tokenData.user)
					}
					
					uni.hideLoading()
					console.log('自动登录成功')
					
					// 启动后台token保活
					this.startTokenAutoRefresh()
					// 跳转到主页
					uni.reLaunch({
						url: '/pages/index/index'
					})
				} else {
					throw new Error('登录响应格式错误')
				}
				
			} catch (error) {
				console.error('自动登录失败，尝试使用刷新令牌续期:', error)
				try {
					// 尝试使用 refreshToken 续期并进入主页
					const newAccessToken = await refreshAccessToken()
					const userStore = useUserStore()
					userStore.setToken(newAccessToken)
					const userInfo = uni.getStorageSync('userInfo')
					if (userInfo) userStore.setUserInfo(userInfo)
					uni.hideLoading()
					this.startTokenAutoRefresh()
					uni.reLaunch({ url: '/pages/index/index' })
				} catch (e2) {
					// 刷新也失败则静默停留在当前页，不再弹出失败提示
					uni.hideLoading()
				}
			}
		},
		// 后台定时刷新token（基于exp提前60秒），并在进入应用时保持活跃
		startTokenAutoRefresh() {
			try {
				// 先确保一次有效
				ensureValidToken()
				// 每5分钟检查一次
				if (this._tokenTimer) clearInterval(this._tokenTimer)
				this._tokenTimer = setInterval(() => {
					ensureValidToken().catch(() => {})
				}, 5 * 60 * 1000)
			} catch (e) {}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/* 全局HTML和body样式 - 确保页面占满整个视口 */
	html, body {
		height: 100%;
		margin: 0;
		padding: 0;
		overflow-x: hidden;
	}

	/* 确保#app容器也占满整个视口 */
	#app {
		height: 100%;
		width: 100%;
		min-height: 100vh;
	}

	/* 响应式CSS变量 */
	:root {
		/* 基础尺寸 */
		--base-font-size: 16px;
		--base-line-height: 1.5;
		--base-spacing: 8px;
		
		/* 颜色系统 */
		--primary-color: #667eea;
		--secondary-color: #764ba2;
		--text-primary: #2c3e50;
		--text-secondary: #7f8c8d;
		--background-primary: #ffffff;
		--background-secondary: #f8f9fa;
		--border-color: #e9ecef;
		
		/* 间距系统 */
		--spacing-xs: 4px;
		--spacing-sm: 8px;
		--spacing-md: 16px;
		--spacing-lg: 24px;
		--spacing-xl: 32px;
		--spacing-xxl: 48px;
		
		/* 圆角系统 */
		--border-radius-sm: 4px;
		--border-radius-md: 8px;
		--border-radius-lg: 12px;
		--border-radius-xl: 16px;
		
		/* 阴影系统 */
		--shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.05);
		--shadow-md: 0 4px 8px rgba(0, 0, 0, 0.1);
		--shadow-lg: 0 8px 16px rgba(0, 0, 0, 0.15);
		
		/* 断点系统 */
		--breakpoint-xs: 480px;
		--breakpoint-sm: 768px;
		--breakpoint-md: 1024px;
		--breakpoint-lg: 1200px;
		--breakpoint-xl: 1440px;
	}

	/* 移动端适配 */
	@media (max-width: 480px) {
		:root {
			--base-font-size: 14px;
			--base-spacing: 6px;
		}
	}

	/* 平板适配 */
	@media (min-width: 481px) and (max-width: 768px) {
		:root {
			--base-font-size: 15px;
			--base-spacing: 7px;
		}
	}

	/* 桌面端适配 */
	@media (min-width: 769px) {
		:root {
			--base-font-size: 16px;
			--base-spacing: 8px;
		}
		
		/* PC端特殊处理 - 确保页面占满整个屏幕 */
		html, body {
			height: 100vh;
			min-height: 100vh;
		}
		
		#app {
			height: 100vh;
			min-height: 100vh;
		}
	}

	/* 大屏幕适配 */
	@media (min-width: 1200px) {
		:root {
			--base-font-size: 18px;
			--base-spacing: 10px;
		}
	}

	/* 全局重置 - 小程序兼容 */
	page, view, text, button, input, textarea, form {
		box-sizing: border-box;
		margin: 0;
		padding: 0;
	}

	/* 基础页面样式 */
	page {
		height: 100vh;
		overflow: hidden;
		font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
		font-size: 32rpx;
		line-height: 1.5;
		color: #2c3e50;
		background-color: #ffffff;
	}

	/* 容器基础样式 */
	.container {
		height: 100vh;
		overflow: hidden;
		max-width: 100%;
		margin: 0 auto;
	}

	/* 响应式容器 */
	.container-fluid {
		width: 100%;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	.container-sm {
		max-width: 540px;
		margin: 0 auto;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	.container-md {
		max-width: 720px;
		margin: 0 auto;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	.container-lg {
		max-width: 960px;
		margin: 0 auto;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	.container-xl {
		max-width: 1140px;
		margin: 0 auto;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	/* 响应式网格系统 */
	.row {
		display: flex;
		flex-wrap: wrap;
		margin-left: -16rpx;
		margin-right: -16rpx;
	}

	.col {
		flex: 1;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}

	/* 响应式列 */
	.col-xs-12 { flex: 0 0 100%; max-width: 100%; }
	.col-xs-6 { flex: 0 0 50%; max-width: 50%; }
	.col-xs-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
	.col-xs-3 { flex: 0 0 25%; max-width: 25%; }

	@media (min-width: 481px) {
		.col-sm-12 { flex: 0 0 100%; max-width: 100%; }
		.col-sm-6 { flex: 0 0 50%; max-width: 50%; }
		.col-sm-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
		.col-sm-3 { flex: 0 0 25%; max-width: 25%; }
	}

	@media (min-width: 769px) {
		.col-md-12 { flex: 0 0 100%; max-width: 100%; }
		.col-md-6 { flex: 0 0 50%; max-width: 50%; }
		.col-md-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
		.col-md-3 { flex: 0 0 25%; max-width: 25%; }
	}

	@media (min-width: 1025px) {
		.col-lg-12 { flex: 0 0 100%; max-width: 100%; }
		.col-lg-6 { flex: 0 0 50%; max-width: 50%; }
		.col-lg-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
		.col-lg-3 { flex: 0 0 25%; max-width: 25%; }
	}

	/* 工具类 */
	.d-none { display: none !important; }
	.d-block { display: block !important; }
	.d-flex { display: flex !important; }
	.d-inline-flex { display: inline-flex !important; }

	/* PC端全屏样式类 */
	.pc-fullscreen {
		height: 100vh !important;
		min-height: 100vh !important;
		width: 100% !important;
		box-sizing: border-box !important;
		overflow-x: hidden !important;
	}

	/* PC端容器样式 */
	.pc-container {
		height: 100vh !important;
		min-height: 100vh !important;
		width: 100% !important;
		display: flex !important;
		flex-direction: column !important;
		box-sizing: border-box !important;
		overflow-x: hidden !important;
	}

	/* PC端内容区域样式 */
	.pc-content {
		flex: 1 !important;
		display: flex !important;
		flex-direction: column !important;
		min-height: 0 !important;
	}

	@media (min-width: 481px) {
		.d-sm-none { display: none !important; }
		.d-sm-block { display: block !important; }
		.d-sm-flex { display: flex !important; }
		.d-sm-inline-flex { display: inline-flex !important; }
	}

	@media (min-width: 769px) {
		.d-md-none { display: none !important; }
		.d-md-block { display: block !important; }
		.d-md-flex { display: flex !important; }
		.d-md-inline-flex { display: inline-flex !important; }
	}

	@media (min-width: 1025px) {
		.d-lg-none { display: none !important; }
		.d-lg-block { display: block !important; }
		.d-lg-flex { display: flex !important; }
		.d-lg-inline-flex { display: inline-flex !important; }
	}

	/* 文本对齐 */
	.text-left { text-align: left !important; }
	.text-center { text-align: center !important; }
	.text-right { text-align: right !important; }

	/* 间距工具类 */
	.m-0 { margin: 0 !important; }
	.mt-0 { margin-top: 0 !important; }
	.mb-0 { margin-bottom: 0 !important; }
	.ml-0 { margin-left: 0 !important; }
	.mr-0 { margin-right: 0 !important; }

	.p-0 { padding: 0 !important; }
	.pt-0 { padding-top: 0 !important; }
	.pb-0 { padding-bottom: 0 !important; }
	.pl-0 { padding-left: 0 !important; }
	.pr-0 { padding-right: 0 !important; }

	/* 响应式间距 */
	.m-1 { margin: 4rpx !important; }
	.m-2 { margin: 8rpx !important; }
	.m-3 { margin: 16rpx !important; }
	.m-4 { margin: 24rpx !important; }
	.m-5 { margin: 32rpx !important; }

	.p-1 { padding: 4rpx !important; }
	.p-2 { padding: 8rpx !important; }
	.p-3 { padding: 16rpx !important; }
	.p-4 { padding: 24rpx !important; }
	.p-5 { padding: 32rpx !important; }

	/* 弹性布局工具类 */
	.flex-row { flex-direction: row !important; }
	.flex-column { flex-direction: column !important; }
	.flex-wrap { flex-wrap: wrap !important; }
	.flex-nowrap { flex-wrap: nowrap !important; }
	.justify-start { justify-content: flex-start !important; }
	.justify-end { justify-content: flex-end !important; }
	.justify-center { justify-content: center !important; }
	.justify-between { justify-content: space-between !important; }
	.justify-around { justify-content: space-around !important; }
	.align-start { align-items: flex-start !important; }
	.align-end { align-items: flex-end !important; }
	.align-center { align-items: center !important; }
	.align-baseline { align-items: baseline !important; }
	.align-stretch { align-items: stretch !important; }

	/* 响应式工具类 */
	@media (max-width: 480px) {
		.hidden-xs { display: none !important; }
		.visible-xs { display: block !important; }
	}

	@media (min-width: 481px) and (max-width: 768px) {
		.hidden-sm { display: none !important; }
		.visible-sm { display: block !important; }
	}

	@media (min-width: 769px) and (max-width: 1024px) {
		.hidden-md { display: none !important; }
		.visible-md { display: block !important; }
	}

	@media (min-width: 1025px) {
		.hidden-lg { display: none !important; }
		.visible-lg { display: block !important; }
	}

	/* 安全区域适配 */
	.safe-area-inset-top {
		padding-top: 0;
	}

	.safe-area-inset-bottom {
		padding-bottom: 0;
	}

	.safe-area-inset-left {
		padding-left: 0;
	}

	.safe-area-inset-right {
		padding-right: 0;
	}

	/* 横屏适配 */
	@media screen and (orientation: landscape) {
		.landscape-hidden {
			display: none !important;
		}
	}

	/* 竖屏适配 */
	@media screen and (orientation: portrait) {
		.portrait-hidden {
			display: none !important;
		}
	}

	/* 高分辨率屏幕适配 */
	@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
		.retina-image {
			image-rendering: -webkit-optimize-contrast;
			image-rendering: crisp-edges;
		}
	}

	/* 触摸设备适配 */
	@media (hover: none) and (pointer: coarse) {
		.touch-target {
			min-height: 44px;
			min-width: 44px;
		}
	}

	/* 非触摸设备适配 */
	@media (hover: hover) and (pointer: fine) {
		.hover-effect:hover {
			transform: translateY(-2px);
			box-shadow: var(--shadow-lg);
		}
	}

	/* 打印样式 */
	@media print {
		.no-print {
			display: none !important;
		}
	}
</style>
